import re

text = "Hello world hello"
pattern = r"Hello"
res = re.search(pattern,text)
print(res)
text1 = "world"
res = re.search(pattern,text1)
print(res)
# 元字符 . 可以匹配任何的字符除了换行符
text = "cat sat hat"
pattern = r".at"
res = re.findall(pattern,text)
print(res)

text = "cat sat \nat"
res = re.findall(pattern,text)
print(res)
# *表示前边的字符出现一次或多次
text = "cat caat caaat ct cbt"
pattern = r"ca*t"
res = re.findall(pattern,text)
print(res)
# +表示出现1次或多次
pattern = r"ca+t"
res = re.findall(pattern,text)
print(res)
# ?表示出现0次或一次
pattern = r"ca?t"
res = re.findall(pattern,text)
print(res)
# 控制数量 {m,n} 出现至少m次，至多n {n}只出现n次
pattern = r"ca{2,3}t"
res = re.findall(pattern,text)
print(res)
pattern = r"ca{3}t"
res = re.findall(pattern,text)
print(res)
# 贪婪和非贪婪
text = "abcabc"
pattern = r"a.*c"
res = re.findall(pattern,text)
print(res)
pattern = r"a.*?c"
res = re.findall(pattern,text)
print(res)
# [] 可以匹配任意出现在[]内部的字符
text = "abcdefg"
pattern = r"[a-e]" # [a-zA-Z0-9_]
res = re.findall(pattern,text)
print(res)
# [^] 排除括号中的内容
pattern = r"[^a-e]"
res = re.findall(pattern,text)
print(res)
# 开头^ 结尾$
text = "152000012341"
pattern = r"^[0-9]{11}$"
res = re.findall(pattern,text)
print(res)

text = "刘伟 张三 刘伟"
pattern = r"刘伟"
res = re.findall(pattern,text,re.U)
print(res)